package com.hz.sdka.ada;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;

import com.anythink.interstitial.api.ATInterstitial;
import com.hz.sdka.commona.SDKContext;
import com.hz.sdka.commona.api.interstitial.OnHZInterstitialListener;
import com.hz.sdka.commona.bean.HZAdError;
import com.hz.sdka.commona.bean.HZAdInfo;
import com.hz.sdka.sdka.AdInterstitiala;


/**
 * topon  全屏视屏和插屏是同一个广告类
 */
public class Interstitiala extends AdBasea {
    private static Interstitiala instance = new Interstitiala();
    private String TAG = "TopOnAd";
    private AdInterstitiala hzInterstitialAd;
    private AdListenera adListener;
    private int time = 30 * 1000;
    private int failCount = 0;

    public static Interstitiala getInstance() {
        return instance;
    }

    private Handler handler = new Handler(Looper.getMainLooper());

    private Interstitiala() {

    }


    public void showAd(Activity activity, String placeId, String scenarioId) {
        if (!TextUtils.isEmpty(scenarioId)) {
            ATInterstitial.entryAdScenario(placeId, scenarioId);
        }
        if (hzInterstitialAd == null) {
            load(placeId);
        }
        failCount = 0;
        if (adListener != null) adListener.adRequest();
        if (hzInterstitialAd != null && hzInterstitialAd.isReady()) {
            if (adListener != null) adListener.adReady();
            hzInterstitialAd.show(activity, scenarioId);
        } else {
            load(placeId);
        }
    }

    public void setAdListener(AdListenera adListener) {
        this.adListener = adListener;
    }

    /*判断广告是否准备好*/
    public boolean isReady() {
        if (hzInterstitialAd != null)
            return hzInterstitialAd.isReady();
        else return false;
    }

    /**
     * 获取广告价格
     */
    public double getEcpm() {
        if (hzInterstitialAd != null) {
            hzInterstitialAd.getEcpm();
        }
        return 0.00;
    }

    public void load(String placeId) {
        if (hzInterstitialAd == null) {
            hzInterstitialAd = new AdInterstitiala(SDKContext.getInstance().getContext(), placeId);
        }
        hzInterstitialAd.setListener(new OnHZInterstitialListener() {
            @Override
            public void onInterstitialLoadFailed(HZAdError hzAdError) {
                Log.i(TAG, "onInterstitialLoadFailed:\n" + hzAdError.printStackTrace());
                if (adListener != null) adListener.adError(hzAdError);
                postLoad();
            }

            @Override
            public void onInterstitialLoaded() {
                Log.i(TAG, "onInterstitialAdLoaded");
                if (adListener != null) adListener.adLoaded();
                failCount = 0;
            }

            @Override
            public void onInterstitialShown(HZAdInfo hzAdInfo) {
                Log.i(TAG, "onInterstitialAdShow:\n");
                if (adListener != null) adListener.adShow();
            }

            @Override
            public void onInterstitialShowFailed(HZAdError hzAdError) {
                Log.i(TAG, "onInterstitialAdLoadFail:\n" + hzAdError.printStackTrace());
                if (adListener != null) adListener.adError(hzAdError);
            }

            @Override
            public void onInterstitialClosed() {
                Log.i(TAG, "onInterstitialAdClose:\n");
                if (adListener != null) adListener.adClose();
                load(placeId);
            }

            @Override
            public void onInterstitialEnd() {

            }

            @Override
            public void onInterstitialClicked() {

            }

            @Override
            public void onSkip() {

            }
        });
        load();
    }

    /**
     * 加载广告
     */
    private void load() {
        if (hzInterstitialAd != null) {
            if (adListener != null) adListener.adLoading();
            if (!isReady()) hzInterstitialAd.load();
        }
    }

    /**
     * 延迟load
     */
    private void postLoad() {
        if (failCount < 3 && hzInterstitialAd != null && !isAdPostLoading) {
            isAdPostLoading = true;
            handler.postDelayed(() -> {
                failCount++;
                if (!isReady()) {
                    isAdPostLoading = false;
                    hzInterstitialAd.load();
                }
            }, time);
        }
    }
}
